// pages/my/getMobile/getMobile.js
const app = getApp();
var util = require('../../util/util.js');
var timeIn = null;//倒计时函数
Page({

  /**
   * 页面的初始数据
   */
  data: {
    phoneNumber: null,
    sms: null,
    time: 60,//发送短信计时器
    btnTxt: "获取验证码",//倒计时
    disabled: false,//发送短信按钮
    bgcolor: "#FF9C00",
    color: "#fff",
  },
  //手机号码变化
  telChange(e){
    let tel = e.detail.value;
    this.setData({
      phoneNumber: tel
    })
  },
  //获取验证码
  getSms(e){
    var that = this;
    let tel = e.target.dataset.tel;
    //console.info(e);
    let regIn = /^1([358][0-9]|4[579]|66|7[0135678]|9[89])[0-9]{8}$/;
    //是否为空
    if (!tel) {
      wx.showToast({
        title: "手机号码不能为空~",
        icon: "none"
      })
      return
    }else if (!regIn.test(tel)) {//格式不对
      wx.showToast({
        title: "手机号码有误~",
        icon: "none"
      })
      return
    }else{
      //请求数据
      that.setData({
        disabled: true,
        bgcolor: "#FFBE60",
        color:"#fff",
      })
      that.timer();
      var url = app.globalData.api + "/sms/checkCode"
      wx.request({
        url: url,
        method: "post",
        data: { "phoneNumber": tel },
        success: function (res) {
          // console.info(res);
          if (res.data.code == 0) {
            //成功
            wx.showToast({
              title: "发送成功~",
              icon: "success"
            })
          } else {
            //失败
            wx.showToast({
              title: res.data.msg,
              icon: "none"
            })
            return
          }
        }, fail: function (err) {
          wx.showToast({
            title: "系统异常，请稍后重试~",
            icon: "none"
          })
        }
      })
    }
  },
  timer() {
    var that = this;
    var time = that.data.time;
    timeIn = setInterval(function(){
      time--;
      that.setData({
        btnTxt: "获取验证码(" + time + ")"
      })
      if(time<=0){
        clearInterval(timeIn);
        that.setData({
          time: 60,
          btnTxt: "获取验证码",
          disabled: false
        })
      }
    },1000)
  },
  //绑定
  reg(e) {
    // 18070185540   908925
    let form = e.detail.value;
    if (util.changeProperties(form)) {
      wx.showToast({
        title: "请填写手机号/验证码",
        icon: "none"
      })
      return
    }else {
      var url = app.globalData.api + "/wechat/user/register/" + form.sms;
      var openId = (wx.getStorageSync('openId'));
      wx.request({
        url: url,
        method: "post",
        data: { "phoneNumber": form.phoneNumber, "openid": openId},
        success: function (res) {
          // console.info(res.data);
          if (res.data.code == 0) {
            //成功
            //设置缓存
            wx.setStorageSync('token', res.data.token);
            wx.setStorageSync('userId', res.data.userId);
            //弹层提示绑定成功
            wx.showToast({
              title: "绑定成功~",
              icon: "success"
            })
            setTimeout(() => {
              wx.navigateBack()
            }, 1500);
          } else {
            //失败
            wx.showToast({
              title: res.data.msg,
              icon: "none"
            })
            return
          }
        }, fail: function (err) {
          wx.showToast({
            title: "系统异常，请稍后重试~",
            icon: "none"
          })
        }
      })
    }

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})